//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pss;

import com.aidong.dao.wills.PssCheckListDao;
import com.aidong.dao.wills.PssCheckListHisDao;
import com.aidong.dao.wills.PssLossOrderDao;
import com.aidong.dao.wills.PssLossOrderListDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.model.wills.CheckProducts;
import com.aidong.model.wills.PssCheckList;
import com.aidong.model.wills.PssCheckListHis;
import com.aidong.model.wills.PssCheckProduct;
import com.aidong.util.ExportXls;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"purcCheckList"})
public class PurcCheckListController extends BaseController {
    @Resource
    PssCheckListDao pssCheckListDao;
    @Resource
    PssCheckListHisDao pssCheckListHisDao;
    @Resource
    PssProductStockDao pssProductStockDao;
    @Resource
    PssLossOrderListDao pssLossOrderListDao;
    @Resource
    PssLossOrderDao pssLossOrderDao;

    public PurcCheckListController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("pss/purccheck/list")).addObject("shopName", this.getTypeList("shopName"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode) {
        List lstCheck = this.pssCheckListDao.queryCheckList(shopCode);
        Iterator mv = lstCheck.iterator();

        while(mv.hasNext()) {
            PssCheckProduct pc = (PssCheckProduct)mv.next();
            pc.setDifQuantity(Double.valueOf(pc.getCheckQuantityhis().doubleValue() - pc.getStockQuantityhis().doubleValue()));
        }

        ModelAndView mv1 = new ModelAndView("pss/purccheck/_list");
        mv1.addObject("list", lstCheck);
        mv1.addObject("shopCode", shopCode);
        mv1.addObject("shopName", this.getTypeList("shopName"));
        mv1.addObject("productUnit", this.getTypeList("productUnit"));
        return mv1;
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@RequestParam(value = "shopCode",required = false) String shopCode, CheckProducts products) {
        if(this.chkNull(shopCode)) {
            return this.getResult(false, "请输入盘点部门");
        } else if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请先查询盘点部门的商品数据");
        } else {
            PssCheckList pcl = null;
            ArrayList lst = new ArrayList();
            Iterator e = products.getLstProduct().iterator();

            while(e.hasNext()) {
                PssCheckProduct pc = (PssCheckProduct)e.next();
                pcl = new PssCheckList();
                if(!this.chkNull(pc.getProductCode())) {
                    pcl.setCheckShop(shopCode);
                    pcl.setProductCode(pc.getProductCode());
                    pcl.setQuantity(pc.getCheckQuantity());
                    pcl.setCheckDate(new Date());
                    pcl.setCreateUser(this.getLoginUserCode());
                    pcl.setCreateTime(new Date());
                    pcl.setUpdateTime(new Date());
                    lst.add(pcl);
                }
            }

            try {
                this.pssCheckListDao.addCheckList(lst);
                this.addUserLog("商品盘点出异常", "编号");
            } catch (Exception var7) {
                this.log.error("edit setting", var7);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"_hissubmit"})
    @ResponseBody
    public String _hissubmit(@RequestParam(value = "shopCode",required = false) String shopCode, CheckProducts products) {
        if(this.chkNull(shopCode)) {
            return this.getResult(false, "请输入盘点部门");
        } else if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请先查询盘点部门的商品数据");
        } else {
            PssCheckListHis pcl = null;
            ArrayList lst = new ArrayList();
            Iterator e = products.getLstProduct().iterator();

            while(e.hasNext()) {
                PssCheckProduct pc = (PssCheckProduct)e.next();
                pcl = new PssCheckListHis();
                if(!this.chkNull(pc.getProductCode())) {
                    pcl.setCheckShop(shopCode);
                    pcl.setProductCode(pc.getProductCode());
                    pcl.setCheckQuantity(pc.getCheckQuantity());
                    pcl.setStockQuantity(pc.getStockQuantity());
                    pcl.setCheckDate(new Date());
                    pcl.setCreateUser(this.getLoginUserCode());
                    pcl.setCreateTime(new Date());
                    pcl.setUpdateTime(new Date());
                    lst.add(pcl);
                }
            }

            try {
                this.pssCheckListDao.addHisAndCheck(lst, this.pssCheckListHisDao);
                this.addUserLog("商品盘点提交出异常", "编号");
            } catch (Exception var7) {
                this.log.error("edit setting", var7);
                return this.getResult(false, "提交发生错误");
            }

            return this.getResult(true, "提交完成");
        }
    }

    @RequestMapping({"_delete"})
    @ResponseBody
    public String _delete(@RequestParam(value = "shopCode",required = false) String shopCode, CheckProducts products) {
        if(this.chkNull(shopCode)) {
            return this.getResult(false, "请输入盘点部门");
        } else if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请先查询盘点部门的商品数据");
        } else {
            PssCheckList pcl = null;
            ArrayList lst = new ArrayList();
            Iterator e = products.getLstProduct().iterator();

            while(e.hasNext()) {
                PssCheckProduct pc = (PssCheckProduct)e.next();
                pcl = new PssCheckList();
                if(!this.chkNull(pc.getProductCode())) {
                    pcl.setCheckShop(shopCode);
                    pcl.setProductCode(pc.getProductCode());
                    pcl.setQuantity(pc.getCheckQuantity());
                    pcl.setCheckDate(new Date());
                    pcl.setCreateUser(this.getLoginUserCode());
                    pcl.setCreateTime(new Date());
                    pcl.setUpdateTime(new Date());
                    lst.add(pcl);
                }
            }

            try {
                this.pssCheckListDao.deleteCheckList(lst);
                this.addUserLog("商品盘点清除出异常", "编号");
            } catch (Exception var7) {
                this.log.error("edit setting", var7);
                return this.getResult(false, "清除发生错误");
            }

            return this.getResult(true, "清除完成");
        }
    }

    @RequestMapping({"_updatestock"})
    @ResponseBody
    public String _updatestock(@RequestParam(value = "shopCode",required = false) String shopCode, CheckProducts products) {
        if(this.chkNull(shopCode)) {
            return this.getResult(false, "请输入盘点部门");
        } else if(products.getLstProduct().size() == 0) {
            return this.getResult(false, "请先查询盘点部门的商品数据");
        } else {
            boolean updateflg = false;
            Iterator e = products.getLstProduct().iterator();

            while(e.hasNext()) {
                PssCheckProduct pc = (PssCheckProduct)e.next();
                if(pc.getDifQuantity().doubleValue() > 0.0D) {
                    updateflg = true;
                }
            }

            if(!updateflg) {
                return this.getResult(false, "没有发生盈亏，无需更新库存");
            } else {
                try {
                    this.pssCheckListDao.updateStock(shopCode, this.getLoginUserCode(), products, this.pssProductStockDao, this.pssLossOrderListDao, this.pssLossOrderDao, this.pssCheckListHisDao);
                    this.addUserLog("商品盘点清除出异常", "编号");
                } catch (Exception var6) {
                    this.log.error("edit setting", var6);
                    return this.getResult(false, "清除发生错误");
                }

                return this.getResult(true, "按盘盈亏数量更新库存完成");
            }
        }
    }

    @RequestMapping({"expt"})
    public ModelAndView expt(@RequestParam(value = "shopCode",required = false) String shopCode, HttpServletResponse response) {
        try {
            List e = this.pssCheckListDao.queryCheckList(shopCode);
            Iterator xls = e.iterator();

            while(xls.hasNext()) {
                PssCheckProduct i$ = (PssCheckProduct)xls.next();
                i$.setDifQuantity(Double.valueOf(i$.getCheckQuantityhis().doubleValue() - i$.getStockQuantityhis().doubleValue()));
            }

            ExportXls xls1 = new ExportXls(response, "商品盘点", "商品盘点");
            xls1.addHead(new String[]{"商品编码", "商品名称", "单位", "当前库存数量", "盘点提交库存数量", "盘点提交数量", "盈亏数量", "当前盘点时间", "盘点提交时间"});
            xls1.setWidth(new int[]{10, 25, 10, 10, 10, 10, 10, 10, 10});
            Iterator i$1 = e.iterator();

            while(i$1.hasNext()) {
                PssCheckProduct puot = (PssCheckProduct)i$1.next();
                xls1.addRow(new Object[]{puot.getProductCode(), this.cache.getValue("ProductcodeList", puot.getProductCode()), this.cache.getValue("productUnit", puot.getUnit()), puot.getStockQuantity(), puot.getStockQuantityhis(), puot.getCheckQuantityhis(), puot.getDifQuantity(), puot.getCheckdate(), puot.getCheckdatehis()});
            }

            xls1.close();
            this.addUserLog("导出商品盘点", e.size() + "条记录");
        } catch (Exception var7) {
            this.log.error("import", var7);
        }

        return null;
    }
}
